package me.zixing.timer.core;

import java.io.File;
import java.net.URL;
import java.util.*;

/**
 * Created by bingh on 2017/10/21.
 */
public class ImageTime implements Iterable<ImageTimeElement> {
    private List<ImageTimeElement> list = new ArrayList<>(8);


    private static class ImageTimeMapper{
        private static Map<String,URL> map = new HashMap<>();
        private static final String ZERO = "0";
        private static final String ONE = "1";
        private static final String TWO = "2";
        private static final String THREE = "3";
        private static final String FOUR = "4";
        private static final String FIVE = "5";
        private static final String SIX = "6";
        private static final String SEVEN = "7";
        private static final String EIGHT = "8";
        private static final String NINE = "9";
        private static final String SPLIT = "-1";

        static{
            map.put(ZERO,ImageTimeMapper.class.getClassLoader().getResource("0.png"));
            map.put(ONE,ImageTimeMapper.class.getClassLoader().getResource("1.png"));
            map.put(TWO,ImageTimeMapper.class.getClassLoader().getResource("2.png"));
            map.put(THREE,ImageTimeMapper.class.getClassLoader().getResource("3.png"));
            map.put(FOUR,ImageTimeMapper.class.getClassLoader().getResource("4.png"));
            map.put(FIVE,ImageTimeMapper.class.getClassLoader().getResource("5.png"));
            map.put(SIX,ImageTimeMapper.class.getClassLoader().getResource("6.png"));
            map.put(SEVEN,ImageTimeMapper.class.getClassLoader().getResource("7.png"));
            map.put(EIGHT,ImageTimeMapper.class.getClassLoader().getResource("8.png"));
            map.put(NINE,ImageTimeMapper.class.getClassLoader().getResource("9.png"));
            map.put(SPLIT,ImageTimeMapper.class.getClassLoader().getResource("split.png"));
            map.put("_",ImageTimeMapper.class.getClassLoader().getResource("-2.png"));
        }

        public static URL getImageByNum(String num){
            return map.get(num);
        }
    }

    public static ImageTime parseTime(){
        ImageTime imageTime = new ImageTime();
        String[] timeElements = TimeComputUtil.computTime();
        imageTime.setHH(
                new ImageTimeElement(
                        ImageTimeMapper.getImageByNum(String.valueOf(timeElements[0].charAt(0)))),
                new ImageTimeElement(
                        ImageTimeMapper.getImageByNum(String.valueOf(timeElements[0].charAt(1)))));
        imageTime.list.add(new ImageTimeElement(ImageTimeMapper.getImageByNum("-1")).
                setX(85).setY(30).setWidth(30).setHeight(30));
        imageTime.setMM(
                new ImageTimeElement(
                        ImageTimeMapper.getImageByNum(String.valueOf(timeElements[1].charAt(0)))),
                new ImageTimeElement(
                        ImageTimeMapper.getImageByNum(String.valueOf(timeElements[1].charAt(1)))));
        imageTime.list.add(new ImageTimeElement(ImageTimeMapper.getImageByNum("-1")).
                setX(180).setY(30).setWidth(30).setHeight(30));
        imageTime.setSS(
                new ImageTimeElement(
                        ImageTimeMapper.getImageByNum(String.valueOf(timeElements[2].charAt(0)))),
                new ImageTimeElement(
                        ImageTimeMapper.getImageByNum(String.valueOf(timeElements[2].charAt(1)))));
        return imageTime;
    }



    void setHH(ImageTimeElement h1,ImageTimeElement h2){
        h1.setX(10).setY(20).setWidth(48).setHeight(48);
        h2.setX(45).setY(20).setWidth(48).setHeight(48);
        list.add(0,h1);
        list.add(1,h2);
    }

    void setMM(ImageTimeElement m1,ImageTimeElement m2){
        m1.setX(105).setY(20).setWidth(48).setHeight(48);
        m2.setX(140).setY(20).setWidth(48).setHeight(48);
        list.add(3,m1);
        list.add(4,m2);
    }

    void setSS(ImageTimeElement s1,ImageTimeElement s2){
        s1.setX(200).setY(20).setWidth(48).setHeight(48);
        s2.setX(235).setY(20).setWidth(48).setHeight(48);
        list.add(6,s1);
        list.add(7,s2);
    }



    @Override
    public Iterator<ImageTimeElement> iterator() {

        return new Iterator<ImageTimeElement>() {
             int point = 0;
             int end = 8;
            @Override
            public boolean hasNext() {
                return point < end? true:false;
            }

            @Override
            public ImageTimeElement next() {
                return list.get(point++);
            }
        };
    }


}
